package cn.interestingshop.web.manager;

import cn.interestingshop.entity.Classify;
import cn.interestingshop.entity.Goods;
import cn.interestingshop.service.classify.ClassifyService;
import cn.interestingshop.service.classify.ClassifyServiceImpl;
import cn.interestingshop.service.goods.GoodsService;
import cn.interestingshop.service.goods.GoodsServiceImpl;
import cn.interestingshop.utils.EmptyUtils;
import cn.interestingshop.utils.JedisUtil;
import cn.interestingshop.utils.Pager;
import cn.interestingshop.web.AbstractServlet;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ClassifyServlet", urlPatterns = "/manager/classify")
public class ClassifyServlet extends AbstractServlet {


    @Override
    public Class getServletClass() {
        return ClassifyServlet.class;
    }


    public String index(HttpServletRequest request, HttpServletResponse response) {

        String currentPage = request.getParameter("currentPage");
        int currentPageNo = EmptyUtils.isEmpty(currentPage) ? 1 : Integer.valueOf(currentPage);

        ClassifyService classifyService = new ClassifyServiceImpl();

        List<Classify> classifies = classifyService.selectAllon(currentPageNo, 8);


        int count = classifyService.selectAllCount();

        Pager pager = new Pager(count, 8, currentPageNo);
        pager.setUrl("manager/classify?action=index");

        request.setAttribute("pager", pager);
        request.setAttribute("classifyList", classifies);

        return "/manager/classify/classifyList";


    }

    public void deleteClassify(HttpServletRequest request, HttpServletResponse response) {
        ClassifyService classifyService = new ClassifyServiceImpl();

        int id = classifyService.deleteById(Integer.parseInt(request.getParameter("id")));

        JSONObject jsonObject = new JSONObject();

        if (id > 0) {
            jsonObject.put("status", 1);
            JedisUtil.getJedis().del("classifyVoList");
            JedisUtil.getJedis().del("notice");

        } else {
            jsonObject.put("status", 0);
            jsonObject.put("message", "删除失败");
        }

        try {
            response.getWriter().print(jsonObject);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void toUpdateClassify(HttpServletRequest request, HttpServletResponse response) {
        ClassifyService classifyService = new ClassifyServiceImpl();

        Classify classify = classifyService.selectOne(Integer.parseInt(request.getParameter("id")));

        JSONObject jsonObject = new JSONObject();

        if (!EmptyUtils.isEmpty(classify)) {
            request.getSession().setAttribute("classify", classify);

            if (classify.getType() == 2) {
                List<Classify> classifies = classifyService.selectOneType(classify.getType() - 1);
                request.getSession().setAttribute("classifyList1", classifies);
            } else if (classify.getType() == 3) {
                List<Classify> classifies = classifyService.selectOneType(classify.getType() - 2);
                request.getSession().setAttribute("classifyList1", classifies);
                List<Classify> classifies2 = classifyService.selectOneType(classify.getType() - 1);
                request.getSession().setAttribute("classifyList2", classifies2);
            }

            jsonObject.put("status", 1);
            JedisUtil.getJedis().del("classifyVoList");
            JedisUtil.getJedis().del("notice");

        } else {
            jsonObject.put("status", 0);
            jsonObject.put("message", "失败");
        }

        try {
            response.getWriter().print(jsonObject);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void modifyClassify(HttpServletRequest request, HttpServletResponse response) {
        int id=Integer.parseInt(request.getParameter("id"));
        String classifyName=request.getParameter("classifyName");
        int classifyLevel1=Integer.parseInt(request.getParameter("classifyLevel1"));
        int classifyLevel2=Integer.parseInt(request.getParameter("classifyLevel2"));

        ClassifyService classifyService=new ClassifyServiceImpl();

        int update = classifyService.update(id, classifyName, classifyLevel1, classifyLevel2);

        JSONObject jsonObject = new JSONObject();

        if (update>0) {
            jsonObject.put("status", 1);
            request.getSession().removeAttribute("classify");
            request.getSession().removeAttribute("classifyList1");
            request.getSession().removeAttribute("classifyList2");
            JedisUtil.getJedis().del("classifyVoList");
            JedisUtil.getJedis().del("notice");

        } else {
            jsonObject.put("status", 0);
            jsonObject.put("message", "失败");
        }

        try {
            response.getWriter().print(jsonObject);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public void toAddClassify(HttpServletRequest request,HttpServletResponse response){

        ClassifyService classifyService=new ClassifyServiceImpl();

        List<Classify> classifies = classifyService.selectOneType(1);
        request.getSession().setAttribute("classifyList1", classifies);
        List<Classify> classifies2 = classifyService.selectOneType(2);
        request.getSession().setAttribute("classifyList2", classifies2);

        request.getSession().setAttribute("classify",new Classify());

    }

    public void addClassify(HttpServletRequest request,HttpServletResponse response){
        int type=Integer.parseInt(request.getParameter("type"));
        String classifyName=request.getParameter("classifyName");
        int classifyLevel1=Integer.parseInt(request.getParameter("classifyLevel1"));
        int classifyLevel2=Integer.parseInt(request.getParameter("classifyLevel2"));


        ClassifyService classifyService=new ClassifyServiceImpl();
        int rs=0;
        rs=classifyService.insert(type,classifyName,classifyLevel1,classifyLevel2);

        JSONObject jsonObject = new JSONObject();

        if (rs>0) {
            jsonObject.put("status", 1);
            request.getSession().removeAttribute("classify");
            request.getSession().removeAttribute("classifyList1");
            request.getSession().removeAttribute("classifyList2");
            JedisUtil.getJedis().del("classifyVoList");
            JedisUtil.getJedis().del("notice");

        } else {
            jsonObject.put("status", 0);
            jsonObject.put("message", "失败");
        }

        try {
            response.getWriter().print(jsonObject);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
